<?php
namespace App\Service\order;

use Elasticsearch\ClientBuilder;
use Hyperf\Guzzle\RingPHP\PoolHandler;
use Swoole\Coroutine;

class OrderService{
    private $client;
    public function __construct(){
        $builder = ClientBuilder::create();
        if (Coroutine::getCid() > 0) {
            $handler = make(PoolHandler::class, [
                'option' => [
                    'max_connections' => 50,
                ],
            ]);
            $builder->setHandler($handler);
        }
        
        $this->client = $builder->setHosts(['http://'.getenv('ES_HOST').':9200'])->build();
    }

    public function createToEs($param){
        $data = [
            'index' => 'orders',
            'id'    => 'orders_id_'.$param['id'],
            'body'  => $param
        ];
        $res = $this->client->index($data);
        print_r($res);
    }
}